<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Model\Admin\Links;
use Illuminate\Support\Facades\Input;

class LinksController extends CommonController
{
  //显示链接管理页 admin/links
	public function index(){
		$data = Links::orderBy('link_order','asc')->get();
		return view('admin.links.index')->with('data', $data);
	}

	// 更新排序
  public function changeOrder(){
  	$input = Input::all();
  	$links = Links::find($input['link_id']);
  	$links->link_order = $input['link_order'];
  	$result = $links->update();
  	if($result){
  		$data = [
  			'status' => '1',
  			'msg'  => '排序更新成功',
  		];
  	} else {
  		 $data = [
  		 	'status' => '0',
  		 	'msg' => '排序更新失败',
  		 ];
  	}
  	return $data;
  }

  // 添加界面显示  get. admin/links/create
  public function create(){
  	return view('admin.links.add');
  }

 	// post. admin/links 添加数据处理
 	public function store(){
    $input = Input::except('_token');
    $result = Links::create($input);
    if($result){
      return redirect('admin/links')->with('success','链接添加成功！');
    }else{
      return back()->with('errors','添加失败！未知错误！');
    }
 	}

  // 修改页面显示 get. admin/links/{links}/edit
  public function edit($link_id){
    $data = Links::find($link_id);
    return view('admin.links.edit',compact('data'));

  }

  // 修改界面数据处理 put admin/links/{links}
  public function update($link_id){
    $input = Input::except('_token','_method');
    $result = Links::where('link_id',$link_id)->update($input);
    if($result !== false){
      return redirect('admin/links')->with('success','修改成功！');
    }else{
      return back()->with('errors','修改失败！');
    }
  }

  public function destroy($link_id){
    $result = Links::where('link_id',$link_id)->delete();
    if($result){
      $data = [
        'status'    =>    '1',
        'msg'       =>    '删除成功！',
      ];
    }else{
      $data = [
        'status'    =>    '0',
        'msg'       =>    '删除失败！',
      ];
    }
    return $data;
  }

  //界面显示
	public function show(){

	}
}
